package nix.jdbc;

import java.io.IOException;
import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


public class AServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			
			/*
			 * Tomcat的conf\Catalina\localhost下  java-web-demo.xml
			 * 
			 <?xml version="1.0" encoding="utf-8" ?>

			<Context>
				<Resource name="jdbc/dataSource"
					factory="org.apache.naming.factory.BeanFactory"
					type="com.mchange.v2.c3p0.ComboPooledDataSource"
					
					jdbcUrl="jdbc:mysql://localhost:3306/test"
					driverClass="com.mysql.jdbc.Driver"
					user="root"
					password="race"
					
					acquireIncrement="5"
					initialPoolSize="10"
				/>
			</Context>
			 */
			
			// 创建JNDI上下文对象
			Context context = new InitialContext();
			// 查找资源
			DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/dataSource");
			Connection con = dataSource.getConnection();
			System.out.println(con);
			con.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
